package com.is.export.control;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.Response;

import org.apache.poi.hssf.record.MMSRecord;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.hssf.util.HSSFColor.GOLD;
import org.logicalcobwebs.cglib.asm.ClassWriter;

import com.is.control.AttendanceDetailControl;
import com.is.model.AttendanceDetail;
import com.is.model.Student;


/**
 * 数据导出类
 * @date 2012-11-12
 * @author Cyoung
 *
 */
public class Export {
	
	@SuppressWarnings("unchecked")
	public static void ExportExcel(String filename,String sheetName,List ulist,HashMap<String, String> ops,String className)throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException{
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet(sheetName);
		sheet.setColumnWidth(0, 3800); //設置列寬
		HSSFCellStyle normalStyle = wb.createCellStyle();
		normalStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		normalStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
		normalStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
		normalStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		normalStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		normalStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		//normalStyle.setWrapText(true);//是否自動換行
		
		//int colIndex = 1;//控制单元格的位置；
		//int colSize = 0;//控制合并单元格的长度
		CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 0, 0,20);//第一行
		sheet.addMergedRegion(cellRangeAddress);
		HSSFRow row = sheet.createRow(0);
		HSSFCell cell = row.createCell(0);// 在索引0的位置创建单元格（左上端）
		cell.setCellValue(new HSSFRichTextString(sheetName));// 在单元格中输入一些内容，标题
		cell.setCellStyle(normalStyle);
		
		
		
		
		row = sheet.createRow(1);
		cell = null;
		int cellNum = 0;
		for(Map.Entry<String, String> mi:ops.entrySet()){
			cell = row.createCell(cellNum++);
			cell.setCellValue(mi.getValue());
			cell.setCellStyle(normalStyle);
		}
		Iterator uit = ulist.iterator();
		int index=2;
		while(uit.hasNext()){
			Class classS = Class.forName(className);
			//System.out.println(classS.getName());
//			Method[] methods = classS.getMethods();
			Object object =  Class.forName(className).newInstance();
			object = uit.next();
			int newCell = 0;
			row = sheet.createRow(index++);
			for(Map.Entry<String, String> mi:ops.entrySet()){
				Method method = classS.getMethod(mi.getKey());
				String value=(String) method.invoke(object);
				cell = row.createCell(newCell++);
				cell.setCellValue(value);
				cell.setCellStyle(normalStyle);
			}
			
//			Iterator it = ops.entrySet().iterator();
//			while(it.hasNext()){
//				Entry<String,String> mi = (Entry)it.next();
//				Method method = classS.getMethod(mi.getKey());
//				String value=(String) method.invoke(object);
//				cell = row.createCell(newCell++);
//				cell.setCellValue(value);
//				cell.setCellStyle(normalStyle);
//				//System.out.println(mi.getKey()+"   "+entry.getValue());
//			
//		     }
		}
		FileOutputStream out = new FileOutputStream(filename);
		//System.out.println("已经导出");
		wb.write(out);
		out.close();
	}
}
